#include"list.h"
#include<algorithm>

void Test1()//测试构造函数
{
	my_list::list<int> list1;//空的构造

	my_list::list<int> list2(4, -1);//3个元素每个元素为-1

	my_list::list<int> list3(list2.begin(), list2.end());//迭代器区间构造

	my_list::list<int> list4(list2);//拷贝构造

	my_list::list<int> list5(5, -1);

	list5 = list2;//赋值运算符重载

	//普通迭代器访问list2
	my_list::list<int>::iterator  it = list2.begin();
	while (it != list2.end())
	{
		cout << *it << " ";
		it++;
	}

	cout << endl;

	//也可以使用范围for来访问
	for (auto& i : list3)
	{
		cout << i << " ";
	}

	cout << endl;

	//也可以使用范围for来访问
	for (auto& i : list4)
	{
		cout << i << " ";
	}

	cout << endl;

	my_list::print_list(list5);
}

void Test2()//头插和头删
{
	my_list::list<int> list1;

	for (int i = 0; i < 10; ++i)
		list1.push_front(i);//依次头插

	cout << "list1: ";
	for (auto& i : list1)
	{
		cout << i << " ";
	}
	cout << endl;

	list1.pop_front();//头删
	list1.pop_front();//头删

	cout << "list1: ";
	for (auto& i : list1)
	{
		cout << i << " ";
	}
	cout << endl;
}

void Test3()//尾插和尾删
{
	my_list::list<int> list1;

	for (int i = 0; i < 10; ++i)
		list1.push_back(i);//依次尾插
	cout << "list1: ";
	for (auto& i : list1)
	{
		cout << i << " ";
	}
	cout << endl;

	list1.pop_back();//尾删
	list1.pop_back();//尾删

	cout << "list1: ";
	for (auto& i : list1)
	{
		cout << i << " ";
	}
	cout << endl; 
}

void Test4()//反向迭代器
{
	my_list::list<int> list1;

	for (int i = 0; i < 10; ++i)
		list1.push_back(i);//依次尾插元素进入list1容器中

	//先正向迭代器正常访问
	my_list::list<int>::iterator it1 = list1.begin();//调用正向迭代器的begin函数，得到访问的开始迭代器

	cout << "list1: ";
	while (it1 != list1.end())
	{
		cout << *it1 << " ";
		it1++;
	}
	cout << endl;
	//反向迭代器访问
	my_list::list<int>::reverse_iterator it2 = list1.rbegin();//调用反向迭代器的rbegin函数，得到访问的开始迭代器

	cout << "list1 r: ";
	while (it2 != list1.rend())
	{
		cout << *it2 << " ";
		it2++;
	}
	cout << endl;

}

void Test5()//insert and erase display
{
	my_list::list<int> list1;

	for (int i = 0; i < 10; ++i)
		list1.push_back(i);//依次尾插元素进入list1容器中
	
	my_list::print_list(list1);//打印

	list1.insert(list1.begin(),1000);//头插一个1000
	my_list::print_list(list1);//打印
	
	list1.insert(list1.end(), 1000);//尾插一个1000
	my_list::print_list(list1);//打印

	list1.erase(list1.begin());//头删
	my_list::print_list(list1);//打印

	list1.erase(--list1.end());//尾删
	my_list::print_list(list1);//打印

	//auto it = std::find<my_list::list<int>::iterator,int>(list1.begin(),list1.end(), 8);//在list1对象中找一个值为8的元素，并返回它的迭代器
	//if (it != list1.end())
	//  list1.erase(it);//删除元素8
	//my_list::print_list(list1);//打印

	//it = std::find(list1.begin(), list1.end(), 10);//在list1对象中找一个值为10的元素，并返回它的迭代器
	//if(it != list1.end())
	//  list1.erase(it);//删除元素10
	//my_list::print_list(list1);//打印

	////头插一个999，再尾插一个999，看find函数会返回谁的迭代器
	//list1.push_front(999);
	//list1.push_back(999);
	//my_list::print_list(list1);//打印

	//it = std::find(list1.begin(), list1.end(), 999);//在list1对象中找一个值为10的元素，并返回它的迭代器
	//if (it != list1.end())
	//	list1.erase(it);//删除元素10
	//my_list::print_list(list1);//打印

}

void Test6()//删除导致list迭代器失效
{
	my_list::list<int> list1;

	for (int i = 0; i < 10; ++i)
		list1.push_back(i);

	my_list::print_list(list1); 

	auto it = list1.begin();
	while (it != list1.end())
	{
		list1.erase(it);//后置++，it++的返回值是it,但是自己的值已经改变了，erase传进去的是operator++的返回值,
		it++;
	}

	my_list::print_list(list1);
}

void Test7()//clear函数，size
{
	my_list::list<int> list1;
	for (int i = 0; i < 10; ++i)
		list1.push_back(i);

	cout << list1.size() << endl;

	list1.clear();
	cout << list1.size() << endl;
}

void Test8()//swap函数
{
	list<int> list1;
	for (int i = 0; i < 10; ++i)
		list1.push_back(i);

	list<int> list2;
	for (int i = 15; i < 25; ++i)
		list2.push_back(i);
	my_list::print_list(list1);
	my_list::print_list(list2);

	std::swap(list1, list2);
	my_list::print_list(list1);
	my_list::print_list(list2);
}

void Test9()
{
	srand(time(0));
	const int N = 1000000;

	list<int> lt1;

	vector<int> v;

	for (int i = 0; i < N; ++i)
	{
		auto e = rand();
		lt1.push_back(e);
		v.push_back(e);
	}

	int begin1 = clock();//vector中排序
	// 
	sort(v.begin(), v.end());
	int end1 = clock();

	int begin2 = clock();//list排序
	lt1.sort();
	int end2 = clock();

	printf("vector sort:%d\n", end1 - begin1);
	printf("list sort:%d\n", end2 - begin2);
}

void Test10()
{
	srand(time(0));
	const int N = 1000000;

	list<int> lt1;
	list<int> lt2;

	for (int i = 0; i < N; ++i)
	{
		auto e = rand();
		lt1.push_back(e);
		lt2.push_back(e);
	}

	int begin1 = clock();//先把数据拷贝到vector,排完序后再拷贝回list的时间
	// vector

	vector<int> v(lt2.begin(), lt2.end());
	// 
	sort(v.begin(), v.end());

	// lt2
	lt2.assign(v.begin(), v.end());

	int end1 = clock();

	int begin2 = clock();//单纯的list的排序
	lt1.sort();
	int end2 = clock();

	printf("list copy vector sort copy list sort:%d\n", end1 - begin1);
	printf("list sort:%d\n", end2 - begin2);
}

int main()
{
	Test4();
	return 0;
}